package com.atguigu.srb.base.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;


/*
    根据mybatis-plus自动填充时间戳
    如果用数据库自己生成的时间戳，可能会导致时区误差的问题
 */

@Component  //让service-core启动类扫描到当前类
public class srbTimeHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        if (metaObject.hasSetter("createTime")){
            metaObject.setValue("createTime",new Date());
        }
        if (metaObject.hasSetter("updateTime")){
            metaObject.setValue("updateTime",new Date());
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (metaObject.hasSetter("updateTime")){
            metaObject.setValue("updateTime",new Date());
        }
    }
}
